package com.hqyj.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.mapper.TicketMapper;
import com.hqyj.pojo.Ticket;
import com.hqyj.serviceInf.TicketServiceInf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ticket")
public class TicketController {


    @Autowired
    private TicketServiceInf ticketServiceInf;

    //全查
    @RequestMapping("/ticketList")
    @ResponseBody
    public Map<String,Object> selectAll(@RequestParam(defaultValue = "1", required = false)int pageNum,
                                          @RequestParam(defaultValue = "3", required = false)int pageSize,
                                          @RequestParam(defaultValue = "")String ticketname)
    {
//        System.out.println(ticketname);
        return ticketServiceInf.selectAll(pageNum,pageSize,ticketname);
    }

    //添加
    @RequestMapping("/addTicket")
    @ResponseBody
    public String addTicket(@RequestBody Ticket ticket){
        System.out.println(ticket);
        return ticketServiceInf.insertTicket(ticket)>0?"redirect:ticketList":"tickets_add";
    }
    //查询一条
    @RequestMapping("/showTicket")
    public String showTicket(int ticketid, Model model){
        Ticket ticket = ticketServiceInf.showTicketById(ticketid);
        model.addAttribute("ticket",ticket);
        return ("ticket_update");
    }
    //修改
    @RequestMapping("/updateTicket")
    @ResponseBody
    public Map<String, Object> updateTicket(@RequestBody Ticket ticket){
        System.out.println(ticket);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", -1);
        map.put("message", "修改失败");
        if(ticketServiceInf.updateTicket(ticket) > 0){
            map.put("code", 200);
            map.put("message", "修改成功");
            map.put("url", "/ticket/ticketList");
        }
        return map;

    }
    //删除
    @RequestMapping("/delateTicket")
    public String delateTicket(int ticketid){
        return ticketServiceInf.delateTicket(ticketid)>0?"redirect:ticketList":"error";
    }
    //页面通用跳转方法
    @RequestMapping("{page}")
    public String toPage(@PathVariable()String page){
        return page;
    }

}
